package com.ruoyi.web.controller.kaoyan;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kaoyan.domain.EducationCategory;
import com.ruoyi.kaoyan.domain.EducationForum;
import com.ruoyi.kaoyan.service.EducationCategoryService;
import com.ruoyi.kaoyan.service.EducationForumService;
import com.ruoyi.system.service.ISysFileInfoService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.kaoyan.vo.ForumVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 后台论坛动态信息管理
 * 包括帖子问答、每日心情变化、考研感想三个分类
 *
 * @author hhr
 */
@Api(tags = "后台动态信息管理审核", value = "后台动态信息管理审核")
@RestController
@RequestMapping("/education/forum")
public class EducationForumController extends BaseController {

    @Autowired
    private EducationForumService forumService;
    @Autowired
    private EducationCategoryService categoryService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysFileInfoService fileInfoService;

    @ApiOperation("帖子动态列表")
    @PostMapping("/list")
    public AjaxResult forumList(String title, Long categoryId, Integer flag) {
        LambdaQueryWrapper<EducationForum> queryWrapper = new LambdaQueryWrapper<EducationForum>()
                .like(StringUtils.isNotBlank(title), EducationForum::getTitle, title);
        if (StringUtils.isNotNull(categoryId)) {
            queryWrapper.eq(true, EducationForum::getCategoryId, categoryId);
        }
        if (StringUtils.isNull(flag)){
            queryWrapper.isNull(EducationForum::getVerFlag);
        }else{
            queryWrapper.eq(EducationForum::getVerFlag, flag);
        }
        Page<EducationForum> page = forumService.page(startPagePlus(), queryWrapper);
        List<EducationForum> forumList = page.getRecords();
        Set<Long> categoryIdSet = forumList.stream().map(EducationForum::getCategoryId).collect(Collectors.toSet());
        Set<Long> userIdSet = forumList.stream().map(EducationForum::getUserId).collect(Collectors.toSet());
        categoryIdSet.add(0L);
        List<EducationCategory> categoryList = categoryService.listByIds(categoryIdSet);
        Map<Long, String> categoryNameMap = categoryList.stream().collect(Collectors.toMap(EducationCategory::getId, EducationCategory::getName));
        List<SysUser> sysUserList = userService.list(new LambdaQueryWrapper<SysUser>()
                .select(SysUser::getUserId, SysUser::getUserName).in(StringUtils.isNotEmpty(userIdSet), SysUser::getUserId, userIdSet));
        Map<Long, String> userNameMap = sysUserList.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getUserName));
        List<ForumVo> forumVoList = forumList.stream().map(e -> new ForumVo(e, categoryNameMap, userNameMap)).collect(Collectors.toList());
        return AjaxResult.ok().page(page, forumVoList).build();
    }

    @ApiOperation("帖子内容详情")
    @PostMapping("/detail")
    public AjaxResult detail(Long id) {
        EducationForum forum = forumService.getById(id);
        String imageGroupId = forum.getImageGroupId();
        String filePath = "";
        if (StringUtils.isNotBlank(imageGroupId)){
            filePath = fileInfoService.getFilePathByGroupId(imageGroupId);
        }
        EducationForum educationForum = EducationForum.builder()
                .content(forum.getContent())
                .imgPath(filePath)
                .build();
        return AjaxResult.ok().proPut("detail", educationForum).build();
    }

    @ApiOperation("帖子审核")
    @PostMapping("/verFlag")
    public AjaxResult verFlag(Long id, Integer flag){
        EducationForum forum = forumService.getById(id);
        if (StringUtils.checkObjAllFieldsIsNull(forum)){
            return AjaxResult.error("帖子不存在");
        }
        forum.setVerFlag(flag);
        boolean update = forumService.updateById(forum);
        return toAjax(update);
    }

}
